﻿@{
   
}
@section head{
    <link href="@Url.Content("~/Js/Validform_v5.3.2/css/style.css")" rel="stylesheet" type="text/css" />
    <script src="@Url.Content("~/Js/Validform_v5.3.2/js/Validform_v5.3.2_min.js")" type="text/javascript"></script>
}
<form  id="form1" action="@Url.Action("RoleSave")" method="post" name="form1">
<div class="rightbar">
    <p class="righttit">
        <span id="updateTitle">角色编辑</span></p>
    <div class="rightshow">
        <table width="80%" border="0" cellspacing="0" cellpadding="0" class="m20 edittable">
            <tr>
                <th width="9%" height="34">
                    <p>
                        角色名称：</p>
                </th>
                <td colspan="2">
                    <input type="text" class="text" id="RoleName" name="RoleName" value="@Model.RoleName" style="width:150px"/>
                    <span class="Validform_checktip"></span>
                </td>
            </tr>
            <tr>
                <th width="9%" height="34">
                    <p>
                        权限分配：</p>
                </th>
                <td colspan="2">
                    <table width="100%">
                        @helper renderTree()
                            {
                                List<string> actionCheckList = new List<string>();
                                if (!string.IsNullOrEmpty(@Model.ActionCollection))
                                {
                                    actionCheckList = @Model.ActionList;
                                }
                                int num = 0;
                                for (int i = 0; i < ViewBag.ControllerList.Count; i++)
                                {
                                    if (i % 4 == 0)
                                    {
                            @MvcHtmlString.Create("<tr>")
                                    }
                            <td valign="top" style="padding: 5px 8px">
                                <h4>@ViewBag.ControllerList[i].Description</h4>
                                <ul>
                                    @{List<SYH.System.Model.SystemAction> actionList = (ViewBag.ActionList as List<SYH.System.Model.SystemAction>).FindAll((e) => { return e.ControllerId == ViewBag.ControllerList[i].Id; });}
                                    @foreach (var item in actionList)
                                    {
                                        num = 0;
                                        for (int j = 0; j < actionCheckList.Count; j++)
                                        {
                                            if (actionCheckList[j].Contains(@ViewBag.ControllerList[i].Controller + "," + @item.Action))
                                            {
                                        <li>
                                            <input type="checkbox" class="actionSwitch" controllerName="@ViewBag.ControllerList[i].Controller" @(actionCheckList[j].Contains(@ViewBag.ControllerList[i].Controller + "," + @item.Action) ? "checked" : "") value="@item.Action" />@item.Description</li>
                                                num = 1;
                                                break;
                                            }
                                        }
                                        if (num == 0)
                                        {
                                        <li>
                                            <input type="checkbox" class="actionSwitch" controllerName="@ViewBag.ControllerList[i].Controller" value="@item.Action" />@item.Description</li>
                                        }
                                    }
                                </ul>
                            </td>
                                    if ((i != 0 && (i + 1) % 4 == 0) || i == ViewBag.ControllerList.Count - 1)
                                    { 
                            @MvcHtmlString.Create("</tr>")
                                    }
                                }
                            }
                        @renderTree()
                    </table>
            </tr>
            <tr>
                <td width="10%">
                    <input type="hidden" name="Id" value="@Model.Id" id="hiddenId"/>
                    <input type="hidden" name="ActionCollection" value="@Model.ActionCollection" />
                    <a href="###" id="btnSubmit" class="sbtn1"><span class="white">确定</span></a>
                </td>
                <td width="83%">
                    <a href="javascript:history.go(-1);" class="sbtn2"><span class="white">取消</span></a>
                </td>
            </tr>
        </table>
    </div>
</div>
</form>
@section Script{
    <script type="text/javascript">
        $(function () {
            //提交验证
            var formSubmit = $("#form1").Validform({
                btnSubmit: "#btnSubmit",
                tiptype: function (msg, o, cssctl) {
                    //msg：提示信息;
                    //o:{obj:*,type:*,curform:*}, obj指向的是当前验证的表单元素（或表单对象），type指示提示的状态，值为1、2、3、4， 1：正在检测/提交数据，2：通过验证，3：验证失败，4：提示ignore状态, curform为当前form对象;
                    //cssctl:内置的提示信息样式控制函数，该函数需传入两个参数：显示提示信息的对象 和 当前提示的状态（既形参o中的type）;
                    if (!o.obj.is("form")) {//验证表单元素时o.obj为该表单元素，全部验证通过提交表单时o.obj为该表单对象;
                        var objtip = o.obj.siblings(".Validform_checktip");
                        cssctl(objtip, o.type);
                        objtip.text(msg);
                    } else {
                        var objtip = o.obj.find("#msgdemo");
                        cssctl(objtip, o.type);
                        objtip.text(msg);
                    }
                },
                ajaxPost: true,
                showAllError: true,
                beforeSubmit: function () {
                    //在验证成功后，表单提交前执行的函数，curform参数是当前表单对象。
                    //这里明确return false的话表单将不会提交;	
                    submitRole();
                },
                callback: function (data) {   //data是object类型
                    if (data == "1") {
                        window.location.href = "/Admin/AdminMgr/RoleList";
                    }
                    else if (data == "0") {
                        art.dialog({
                            title: "提示",
                            icon: "warning",
                            content: "操作失败！",
                            time: 2
                        });
                    }
                }
            }).addRule([{
                ele: "#RoleName",
                datatype: "s2-18",
                nullmsg: "请输入角色名！",
                errormsg: "标题至少2个字符,最多18个字符！"
            }]);
        });


        function submitRole() {
            var actionList = [];
            $(".actionSwitch").each(function (index) {
                if ($(".actionSwitch").eq(index).is(":checked")) {
                    var str = $(".actionSwitch").eq(index).attr("controllerName");
                    str = str + "," + $(".actionSwitch").eq(index).val();
                    actionList.push(str);
                }
            });
            $("input[name=ActionCollection]").val(actionList.join("|"));
        }
    </script>
}
